<template>
  <a-locale-provider :locale="zh_CN">
    <div class="app_box" id="app">
      <a-layout-header v-if="showNav" class="header_bar">
        <a class="logo" href="/">
          <img src="../public/logo_w.png" />
        </a>
        <navList></navList>
        <navUser></navUser>
      </a-layout-header>

      <a-layout :class="mainClass" :hidden="hidePage">
        <router-view name="sidebar"></router-view>
        <a-layout class="view_box">
          <a-layout-content class="view_content">
            <router-view class="view_main" :userActivatedNo="userActivatedNo" />
            <a-layout-footer v-if="showNav" :class="'footer '+ (routerFirst=='/settings'?'settings':'')">
              Copyright ©2020
              <a href="https://www.iqiar.com">IQiar.com</a> All rights reserved.
            </a-layout-footer>
          </a-layout-content>
        </a-layout>
      </a-layout>
      <a-layout v-if="!isLogin && hidePage" class="main_box disconnection">
        <div class="tips">...</div>
      </a-layout>
      <a-layout v-if="!network && hidePage" class="main_box disconnection">
        <div class="tips">
          QiarAI未启动！
          <a-icon type="api" />
          <br />请确保已经执行./qiar
        </div>
      </a-layout>
    </div>
  </a-locale-provider>
</template>

<script>
import zh_CN from "ant-design-vue/lib/locale-provider/zh_CN";
import navUser from "./views/sub/NavUser.vue";
import navList from "./views/sub/NavList.vue";
import tools from "./utils/tools";
export default {
  name: "app",
  data() {
    return {
      zh_CN,
      userInfo: {},
      userActivatedNo: false,
      hidePage: true,
      mainClass: "main_box",
      reLogin: false
    };
  },
  components: {
    navUser,
    navList
  },
  watch: {
    $route() {
      this.loadAuth();
      this.loadSetting();
    }
  },
  created() {
    this.loadAuth();
  },
  computed: {
    showNav() {
      return this.isLogin;
    },
    network() {
      return this.$store.state.common.network;
    },
    isLogin() {
      let nowtime = new Date();
      return (
        tools.formatDate(new Date(this.$store.state.common.token.exp)) >=
        tools.formatDate(nowtime)
      );
    },
    routerFirst() {      
      return this.$route.matched[0].path;
    },
  },
  mounted() {
    this.loadSetting();
  },
  methods: {
    loadAuth() {
      if (this.$route.name == "login" || this.$route.name == "register") {
        this.mainClass = "main_box welcome";
      } else {
        this.mainClass = "main_box";
      }
      if (this.$route.name != "login" && this.$route.name != "register") {
        this.$api.auth
          .getUserInfo()
          .then(res => {
            this.userInfo = res;
            this.userActivatedNo = res.activated == 0;
            this.hidePage = false;
            this.reLogin = false;
          })
          .catch(() => {
            this.$store.commit("SET_TOKEN", {});
            if (this.reLogin == false) {
              this.reLogin = true;
              this.$api.auth.autoLogin();
            }
            this.hidePage = !this.network || !this.isLogin;
          });
      } else {
        this.hidePage = false;
      }
    },
    loadSetting() {
      if (this.isLogin) {
        this.$api.setting
          .all()
          .then(() => {})
          .catch(() => {});
      }
    }
  }
};
</script>


<style lang="less">
@import "./theme/init.less";

.app_box {
  position: relative;
  overflow: hidden;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main_box {
  overflow: auto;
  &.ant-layout {
    background: #fff;
  }
  .view_box {
    padding: 20px;
    background: #fff;
    .view_content {
      min-height: 280px;
    }
    .view_main {
      min-height: 100vh;
    }
  }
  &.disconnection {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 30px;
    text-align: center;
    .tips {
      height: 500px;
    }
  }
  &.welcome {
    background: #333;
    .view_box {
      background: #333;
    }
    .wlogo {
      width: 300px;
      height: 100px;
      margin: 20px auto;
      img {
        width: 100%;
        vertical-align: top;
      }
    }
    .password /deep/ input.ant-input {
      border: 0;
    }
  }
}

.header_bar {
  padding: 0;
  //border-bottom: 3px solid #7281a0;
  box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
  //position:fixed;
  z-index: 1;
  width: 100%;
}

.logo {
  height: 32px;
  margin: 12px 0 12px 20px;
  width: 120px;
  display: inline-block;
  img {
    width: 100%;
    vertical-align: top;
  }
}
.side_bar {
  overflow: hidden;
  height: 100%;
}

.menu_list {
  li.menu_item {
    margin: 12px 0;
  }
  span.text {
    display: inline-block;
    overflow: hidden;
    max-width: 70px;
    //text-overflow: ellipsis;//...
    white-space: nowrap;
    vertical-align: middle;
    transition: width 0.2s ease;
    transition: opacity 0.2s ease;
    font-size: 16px;
    opacity: 1;
  }
  i.icon {
    transform: translateX(0px);
    transition: font-size 0.2s ease, transform 0.2s ease;
    vertical-align: middle;
    font-size: 24px;
    line-height: 20px;
  }
}
.view_box .footer {
  border-top: 1px solid #eee;
  background: #fff;
  &.settings{
    padding-left: 0;
  }
}
</style>
